<template>
  <div class="bg-light defalut-set">
    <div class="container-title">基础设置</div>
    <Form ref="form" :label-width="110" :rules="rules" :model="form">
      <FormItem label="优惠券类别" prop="couponDiscountType">
        <RadioGroup v-model="form.couponDiscountType">
          <Radio label="DENOMINATION_COUPON">满减券</Radio>
          <!-- <Radio label="DISCOUNT_COUPON">折扣券</Radio> -->
          <Radio label="CASH_COUPON">现金券</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="优惠券类型">
        <RadioGroup v-model="form.couponType" @on-change="onChangeType">
          <Radio label="1" :disabled="form.couponDiscountType === 'CASH_COUPON'">商品券</Radio>
          <Radio label="2" :disabled="form.couponDiscountType !== 'CASH_COUPON'">工厂券</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="发放渠道" prop="couponSendChannelEnum">
        <RadioGroup v-model="form.couponSendChannelEnum">
          <Radio label="NONE_LIMIT">不限</Radio>
          <Radio label="TIRE_INSURANCE">轮胎保小程序</Radio>
          <Radio label="CLOUD_APP">中策云店</Radio>
          <Radio label="SALESMAN">业务通</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="促销类型">
        <Checkbox v-model="isCouponPromotionTypeSelected" :disabled="form.couponDiscountType !== 'CASH_COUPON'">促销券 (仅创建工厂特价促销可用)</Checkbox>
        <!-- <RadioGroup v-model="" @on-change="onChangePromotion">
            <Radio label="FACTORY_ACTIVE_COUPON"></Radio>
          </!-->
      </FormItem>
      <FormItem label="优惠券名称" prop="couponName">
        <Input v-model="form.couponName" placeholder="请输入优惠券名称" :maxlength="50" class="wp40" />
      </FormItem>
      <FormItem label="优惠券说明" prop="remark">
        <Input v-model="form.remark" placeholder="请输入优惠券说明" class="wp40" :maxlength="120" />
      </FormItem>
      <FormItem v-show="form.couponDiscountType=='DENOMINATION_COUPON' || form.couponDiscountType=='CASH_COUPON'" label="优惠券面额" class="require-couponAdd">
        <Select v-model="form.money" :label-in-value="true" class="wp30" @on-change="onChangeDenomination">
          <Option v-for="item in options.moneyList" :key="item.value" :value="item.value">{{ item.label }}</Option>
        </Select>
        <InputNumber v-show="showNum" v-model="form.diyMoney" :min="0" :max="9999" /> 元
      </FormItem>
      <FormItem label="每个商品能用">
        <InputNumber v-model="form.useQuantityPerProduct" :min="1" :max="999" :precision="0" class="mr" :disabled="form.couponDiscountType=='DENOMINATION_COUPON' ? false : true" />张
        <span class="font-red">(如不设置则每个订单用一张，如为买家领取的券，这里的张数乘以单券金额后的总金额不能大于下单时某个商品的金额，否则无法下单)</span>
      </FormItem>
      <FormItem v-if="form.couponDiscountType === 'DISCOUNT_COUPON'" label="使用条件" class="require-couponAdd">
        <p class="font-red">(最小3折，最大10折,折扣数逐级增加)</p>
        <div>
          <Button v-if="form.couponType==='1'" class="mr" icon="md-add" size="small" type="primary" @click="onAddLevel" />
          打 <InputNumber v-model="form.discountGroupList[0].discount" :min="3" :max="10" /> 折
        </div>
        <div v-for="(item, index) in form.discountGroupList" v-show="index!=0 && form.maxLevel>1" :key="index">
          <Button class="mr" icon="md-remove" size="small" type="primary" @click="onMinLevel(index)" />
          打 <InputNumber v-model="item.discount" :min="3" :max="10" /> 折
        </div>
      </FormItem>
      <FormItem v-if="form.couponDiscountType=='DENOMINATION_COUPON'" label="使用条件" class="require-couponAdd">
        满 <InputNumber v-model="form.useCondition" :min="0" :max="9999" /> 元
      </FormItem>
      <FormItem label="使用期限">
        <div class="inline">
          <Icon v-if="showDate" type="md-radio-button-on" size="16" class="icon item active" @click="onChangeValidityType" />
          <Icon v-else type="md-radio-button-off" size="16" class="icon item" @click="onChangeValidityType" />
          <DatePicker
            v-model="form.validityDate"
            type="datetimerange"
            placement="bottom-start"
            placeholder="请选择使用期限"
            :options="dateOptions"
            class="item last"
          />
        </div>
        <div class="inline">
          <Icon v-if="!showDate" type="md-radio-button-on" size="16" class="icon item active" @click="onChangeValidityType" />
          <Icon v-else type="md-radio-button-off" size="16" class="icon item" @click="onChangeValidityType" />
          <InputNumber v-model="form.validityDays" :min="0" :max="999" /> 天内有效
        </div>
      </FormItem>
      <FormItem v-if="form.couponDiscountType === 'DENOMINATION_COUPON'" label="使用权重">
        <InputNumber v-model="form.weights" style="width: 150px" :min="1" :max="99" />
        <span class="font-red">(1到99的数字，默认1，数字越高越优先使用)</span>
      </FormItem>
      <FormItem label="领劵期限" prop="receiveDate">
        <DatePicker
          v-model="form.receiveDate"
          type="datetimerange"
          placement="bottom-start"
          placeholder="请选择领劵期限"
          :options="dateOptions"
          class="wp40"
        />
      </FormItem>
      <FormItem label="发行量" class="require-couponAdd">
        <InputNumber v-model="form.amount" style="width: 150px" :min="1" :max="99999999" />
        <span>张</span>
      </FormItem>
      <FormItem v-if="form.couponDiscountType === 'DENOMINATION_COUPON'" label="获取方式">
        <div class="receive-method">
          <RadioGroup v-model="form.receiveMethod">
            <Radio label="SELLER_SEND">卖家发放</Radio>
            <Radio label="BUYER_RECEIVE">买家领取</Radio>
          </RadioGroup>
        </div>
      </FormItem>
      <FormItem label="限领">
        <RadioGroup v-model="form.receiveLimitType">
          <Radio label="LIMIT_NONE">不限制</Radio>
          <!-- 选择现金券的时候限领选择不限制，去掉后面限制或者置灰不允许勾选 -->
          <Radio v-show="form.couponDiscountType !== 'CASH_COUPON'" label="LIMIT_ONE">每人限领<InputNumber v-model="form.oneNum" :max="99999999" :min="1" />张</Radio>
          <Radio v-show="form.couponDiscountType !== 'CASH_COUPON'" label="LIMIT_ONE_PER_DAY">每人每天限领<InputNumber v-model="form.dayNum" :max="99999999" :min="1" />张</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem label="设置">
        <Checkbox v-show="form.couponDiscountType !== 'CASH_COUPON'" v-model="form.isPublic">公开</Checkbox>
        <Checkbox v-show="form.couponDiscountType === 'CASH_COUPON'" v-model="form.couponStatus">启用</Checkbox>
      </FormItem>
    </Form>
    <Loader :status="status" @refresh="getDetailData" />
  </div>
</template>
<script>
import { formatDate } from '@/util'
export default {
  /**
   * NONE_LIMIT(0, "不限")
   * TIRE_INSURANCE(1, "轮胎保小程序")
   * CLOUD_APP(2, "中策云店")
   * SALESMAN(3, "业务通")
   */
  data () {
    return {
      status: 'pass',
      id: this.$route.query.id || null,
      couponId: this.$route.query.couponId || null,
      type: this.$route.query.type || null,
      isShowTip: false,
      showNum: false,
      isCouponPromotionTypeSelected: true, //是否选中促销券
      form: {
        couponId: null,
        couponSendChannelEnum: 'CLOUD_APP', // 发放渠道默认中策云店
        useQuantityPerProduct: null, // 每个商品能用几张
        couponDiscountType: 'CASH_COUPON', //优惠券类别
        couponName: '', // 优惠券名称
        remark: '', // 优惠券说明
        denomination: null, // 面额
        _denomination: null,
        money: '',
        weights: 1, // 权重
        diyMoney: null,
        useCondition: null, // 使用条件
        validityType: '1', // 有效期类型
        validityDate: [], // validityStartDate validityEndDate
        validityDays: null, // 有效天数
        receiveDate: [], // 领券时间 receiveStartDate receiveEndDate
        amount: null, // 发行量
        isPublic: true, // 是否公开
        receiveMethod: 'SELLER_SEND', //获取方式
        receiveLimitType: 'LIMIT_NONE', // 限领
        couponType: '2', // 优惠券类型
        couponPromotionType: 'FACTORY_ACTIVE_COUPON', // 促销类型  默认工厂促销券
        couponStatus: true,
        oneNum: null, //每人限领的张数
        dayNum: null, //每人每天限领的张数
        discountGroupList: [{
          group: 1,
          discount: null
        }], //折扣阶层
        maxLevel: 5 //折扣的最多层级
      },
      isTwoDecimals: true,
      showDate: true,
      dateOptions: {
        disabledDate (date) {
          return date && date.valueOf() < Date.now() - 86400000
        }
      },
      options: {
        moneyList: [
          { value: 1, label: 1 },
          { value: 2, label: 5 },
          { value: 3, label: 10 },
          { value: 4, label: 15 },
          { value: 5, label: 20 },
          { value: 6, label: 30 },
          { value: 7, label: 50 },
          { value: 8, label: 100 },
          { value: 9, label: 200 },
          { value: 10, label: 300 },
          { value: 11, label: 500 },
          { value: 12, label: '自定义' }
        ]
      },
      rules: {
        couponDiscountType: [{ required: true, message: '请选择优惠券类别', trigger: 'change' }],
        couponName: [{ required: true, message: '请输入优惠券名称', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入优惠券说明', trigger: 'blur' }],
        amount: [{ required: true, type: 'number', message: '请输入优惠券发行量', trigger: 'blur' }],
        couponSendChannelEnum: [{ required: true, message: '请选择发放渠道', trigger: 'change' }],
        validityDate: [{
          required: true,
          type: 'array',
          message: '请选择使用期限',
          trigger: 'change',
          fields: {
            0: { type: 'date', required: true, message: '请选择使用期限' },
            1: { type: 'date', required: true, message: '请选择使用期限' }
          }
        }]
      }
    }
  },
  watch: {
    'form.couponDiscountType' (val) {
      switch (val) {
        case 'CASH_COUPON': //现金券不让选商品
          this.form.couponType = '2'
          // 如果是现金券,促销券默认选中,别的类型默认不选中
          this.isCouponPromotionTypeSelected = true
          //如果是现金券,公开隐藏并设置成不选中, 别的类型默认选中
          this.form.isPublic = false
          this.form.receiveLimitType = 'LIMIT_NONE'
          this.$emit('complete', '2')
          break
        case 'DENOMINATION_COUPON': //满减券
          this.form.couponType = '1'
          this.isCouponPromotionTypeSelected = false
          this.form.isPublic = true
          this.$emit('complete', '1')
          // if (this.form.receiveMethod === 'SELLER_SEND') { //卖家发放不让选商品
          //   this.$emit('complete', '2')
          // } else {
          //   this.$emit('complete', '1')
          // }
          break
        default:
          //选择折扣券和满减券的时候，优惠券类型工厂券不可选，促销类型置灰不允许勾选
          this.form.couponType = '1'
          this.$emit('complete', '1')
          this.isCouponPromotionTypeSelected = false
          this.form.isPublic = true
          break
      }
    },
    // 'form.receiveMethod': { // 卖家发放改成  可以让选商品
    //   handler: function (val) {
    //     if (val === 'SELLER_SEND') {
    //       this.$emit('complete', '2')
    //     } else {
    //       this.$emit('complete', '1')
    //     }
    //   },
    //   immediate: true
    // },
    'isCouponPromotionTypeSelected' (val) {
      if (val) {
        this.form.couponPromotionType = 'FACTORY_ACTIVE_COUPON'
      } else {
        this.form.couponPromotionType = 'GENERAL_COUPON'
      }
    }
  },
  mounted () {
    if (this.id && this.type !== 'open') { // 通过复制按钮进来的，需要先经过接口，获取一个新的conpouId，然后根据新的couponId去查getDetailData
      this.getNewCouponId()
    } else {
      this.getDetailData()
    }
    this.$emit('complete', this.form.couponType)
  },
  methods: {
    getNewCouponId () { // 通过复制按钮进来
      this.$emit('changeBtn', true)
      this.status = 'loading'
      this.$ajaxPost(`/coupon/coupon_info/copy/${this.id}`).then(data => {
        this.$set(this.form, 'couponId', data)
        this.couponId = data
        this.getDetailData()
      })
    },
    getDetailData () { // 通过couponId来查第一页的基本信息
      if (this.type === 'open' && this.id) { // 通过启用进来的
        this.couponId = this.id
      }
      if (!this.couponId) {
        // let info = JSON.parse(sessionStorage.getItem('couponForm'))
        // if (!info) return
        // this.form.couponId = info.couponId
        // if (!this.form.couponId) return
        return
      }
      this.$emit('changeBtn', true)
      this.status = 'loading'
      this.$ajaxGet(`/coupon/coupon_info/detail/${this.couponId}`).then(data => {
        this.$emit('changeBtn', false)
        this.status = 'pass'
        this.form = data
        this.couponId = data.id
        this.form.couponType = data.couponType === 'COUPON_PLATFORM' ? '2' : '1' //1商品券 2平台券
        this.isCouponPromotionTypeSelected = this.form.couponPromotionType === 'FACTORY_ACTIVE_COUPON' //是否选中促销券
        this.onChangeType(this.form.couponType)
        let flag = true
        if (this.form.couponDiscountType === 'DENOMINATION_COUPON' || this.form.couponDiscountType === 'CASH_COUPON') { //满减券 现金券
          //优惠券面额
          for (let i = 0; i < this.options.moneyList.length; i++) {
            if (this.options.moneyList[i].label === this.form.denomination) {
              this.showNum = false
              this.form.money = this.options.moneyList[i].value
              this.form._denomination = this.options.moneyList[i].label
              this.form.diyMoney = null
              flag = false
              break
            }
          }
          if (flag) {
            this.showNum = true
            this.form.diyMoney = this.form.denomination
            this.form.money = 12
          }
          this.form.discountGroupList = [{
            group: 1,
            discount: null
          }] //没有折扣阶级
        } else { //折扣券
          this.form.denomination = null // 面额
          this.form._denomination = null
          this.showNum = false
          if (this.form.couponType === '2') { //平台券的折扣只有一阶
            this.form.maxLevel = 1
          } else { //商品券最多有5个阶层
            this.form.maxLevel = 5
          }
        }
        //使用期限
        this.form.showDate = this.form.validityType === 'ABSOLUTE_TIME'
        this.showDate = this.form.showDate
        if (this.showDate) {
          this.form.validityDate = [formatDate(this.form.validityStartDate, true), formatDate(this.form.validityEndDate, true)]
          this.form.validityDates = null
        } else {
          this.form.validityDate = []
        }
        //领券期限
        if (this.form.receiveStartDate && this.form.receiveEndDate) {
          this.form.receiveDate = [formatDate(this.form.receiveStartDate, true), formatDate(this.form.receiveEndDate, true)]
        } else {
          this.form.receiveDate = []
        }
        //限领
        if (this.form.receiveLimitType === 'LIMIT_NONE') {
          this.form.dayNum = null
          this.form.oneNum = null
        } else if (this.form.receiveLimitType === 'LIMIT_ONE') {
          this.form.oneNum = this.form.receiveLimitQuantity
          this.form.dayNum = null
        } else {
          this.form.dayNum = this.form.receiveLimitQuantity
          this.form.oneNum = null
        }
        //设置
        this.form.couponStatus = this.form.couponStatus === 'OPEN'
        // 每个商品能用几张券（只有满减券才有）
        this.form.useQuantityPerProduct = this.form.couponDiscountType == 'DENOMINATION_COUPON' ? this.form.useQuantityPerProduct : null
      }).catch(() => {
        this.status = 'error'
      })
    },
    onAddLevel () {
      let level = this.form.discountGroupList.length
      if (level >= this.form.maxLevel) {
        this.$Notice.warning({
          title: '提醒',
          desc: `使用条件的折扣层级最多只有${this.form.maxLevel}级`
        })
        return
      } else {
        level++
      }
      this.form.discountGroupList.push({
        group: level,
        discount: null
      })
    },
    onMinLevel (index) {
      this.form.discountGroupList.splice(index, 1)
      this.form.discountGroupList.forEach((item, i) => {
        item.grounp = i + 1
      })
    },
    postData (type) {
      this.$emit('changeBtn', true)
      this.$refs.form.validate(valid => {
        if (valid) {
          let form = this.form
          // 判断面额，如果money不是12，则一定要选一个，否则自定义面额不能为空
          let error = ''
          let num = null
          this.form.showNum = this.showNum
          if (form.couponDiscountType === 'DENOMINATION_COUPON') { //满减券
            if (!form.money) {
              error = '请选择优惠券面额'
            } else if (form.money === 12 && !form.diyMoney) {
              error = '请输入自定义面额'
            }
            // 优惠券的面额不能大于使用条件的80%
            num = form.showNum ? form.diyMoney : Number(form._denomination)
            if (!form.useCondition) {
              error = '请输入优惠券使用条件'
            } else if ((num > form.useCondition && form.money && form.money != 12) || (num > form.useCondition && form.money === 12 && form.diyMoney)) {
              error = '优惠券的面额不得大于使用条件的金额'
            }
          } else if (form.couponDiscountType === 'DISCOUNT_COUPON') { //折扣券
            if (this.form.couponType == '1') { //商品券
              let disCountIsNull = this.form.discountGroupList.some(item => {
                return item.discount === null || item.discount === ''
              })
              if (disCountIsNull) {
                error = '请输入使用条件的折扣'
              } else {
                let len = this.form.discountGroupList.length
                if (len > 1) {
                  this.form.discountGroupList.forEach((item, i) => {
                    if (!/(^[3-9]\d*(.\d{1,2})?$)|10/.test(item.discount)) {
                      error = '折扣数只能是整数或者最多带两位小数'
                    }
                    if (i === len - 1) {
                      if (item.discount <= this.form.discountGroupList[i - 1].discount) {
                        error = '输入的折扣数要逐级增加'
                      }
                    } else {
                      if (item.discount >= this.form.discountGroupList[i + 1].discount) {
                        error = '输入的折扣数要逐级增加'
                      }
                    }
                  })
                } else {
                  if (!/(^[3-9]\d*(.\d{1,2})?$)|10/.test(this.form.discountGroupList[0].discount)) {
                    error = '折扣数只能是整数或者最多带两位小数'
                  }
                }
              }
            } else { //平台券
              if (!/(^[3-9]\d*(.\d{1,2})?$)|10/.test(this.form.discountGroupList[0].discount)) {
                error = '折扣数只能是整数或者最多带两位小数'
              }
            }
          }
          //判断发行量
          if (!this.form.amount) {
            error = '请输入发行量'
          } else {
            if (!/^-?[1-9]\d*$/.test(this.form.amount)) {
              error = '发行量的张数只能是整数'
            }
          }
          //判断限领的张数
          if (this.form.receiveLimitType === 'LIMIT_ONE') {
            if (!this.form.oneNum) {
              error = '请输入每人限领的张数'
            } else {
              if (!/^-?[1-9]\d*$/.test(this.form.oneNum)) {
                error = '限领的张数只能是整数'
              }
            }
          } else if (this.form.receiveLimitType === 'LIMIT_ONE_PER_DAY') {
            if (!this.form.dayNum) {
              error = '请输入每人每天限领的张数'
            } else {
              if (!/^-?[1-9]\d*$/.test(this.form.dayNum)) {
                error = '限领的张数只能是整数'
              }
            }
          }
          // else if (this.showDate && (form.receiveDate[0] && form.receiveDate[1] && form.receiveDate.length) && (new Date(form.receiveDate[1]).getTime() > new Date(form.validityDate[1].getTime()))) {
          //   error = '领券的结束时间不得大于使用期限的结束时间'
          // }
          // 判断使用期限
          if (this.showDate && (!form.validityDate[0] || !form.validityDate[1] || !form.validityDate.length)) {
            error = '请选择使用期限'
          } else if (!this.showDate && !form.validityDays) {
            error = '请输入有效天数'
          }
          // 判断领券期限
          // if (!form.receiveDate[0] || !form.receiveDate[1] || !form.receiveDate.length) {
          //   error = '请选择领券期限'
          // }
          if (error) {
            this.$emit('changeBtn', false)
            this.$Notice.error({
              title: error
            })
            return
          }
          form.showDate = this.showDate
          this.status = 'loading'
          if (type === 1) {
            form.validityDate = [formatDate(form.validityDate[0], true), formatDate(form.validityDate[1], true)]
            form.receiveDate = [formatDate(form.receiveDate[0], true), formatDate(form.receiveDate[1], true)]
            // sessionStorage.removeItem('originalForm') // 通过接口去获取详情
            let param = {
              couponPromotionType: form.couponPromotionType,
              couponDiscountType: form.couponDiscountType,
              amount: form.amount,
              couponName: form.couponName,
              couponStatus: this.couponId ? (form.couponStatus ? 'OPEN' : 'NO_OPEN') : 'NO_OPEN',
              couponType: 'COUPON_PRODUCT', // 这边只提交商品券
              isPublic: form.isPublic,
              weights: form.weights,
              receiveEndDate: form.receiveDate[1] ? formatDate(new Date(form.receiveDate[1]), true) : '',
              receiveLimitType: form.receiveLimitType, //限领的类型
              receiveLimitQuantity: form.receiveLimitType === 'LIMIT_NONE' ? 0 : form.receiveLimitType === 'LIMIT_ONE' ? form.oneNum : form.dayNum, //传给后台的领的数量
              receiveStartDate: form.receiveDate[0] ? formatDate(new Date(form.receiveDate[0]), true) : '',
              remark: form.remark,
              receiveMethod: form.couponDiscountType === 'DENOMINATION_COUPON' ? form.receiveMethod : 'SELLER_SEND',
              useCondition: form.couponDiscountType == 'DENOMINATION_COUPON' ? form.useCondition : null,
              validityType: form.showDate ? 'ABSOLUTE_TIME' : 'RELATIVE_TIME',
              couponSendChannelEnum: form.couponSendChannelEnum,
              useQuantityPerProduct: form.couponDiscountType == 'DENOMINATION_COUPON' ? form.useQuantityPerProduct : null,
              discountGroupList: form.couponDiscountType === 'DISCOUNT_COUPON' ? form.discountGroupList : []
            }
            if (form.showDate) {
              param.validityStartDate = formatDate(new Date(form.validityDate[0]), true)
              param.validityEndDate = formatDate(new Date(form.validityDate[1]), true)
            } else {
              param.validityDays = form.validityDays
            }
            if (form.showNum) {
              param.denomination = form.couponDiscountType == 'DISCOUNT_COUPON' ? null : form.diyMoney
            } else {
              param.denomination = form.couponDiscountType == 'DISCOUNT_COUPON' ? null : form._denomination
            }
            let url = this.couponId ? '/coupon/coupon_info/modify' : '/coupon/coupon_info/save'
            param.id = this.couponId ? this.couponId : null
            this.$jsonPost(url, param).then(data => {
              this.status = 'pass'
              this.$Notice.success({
                title: '保存成功'
              })
              if (!this.couponId) this.couponId = data
              // sessionStorage.setItem('couponForm', JSON.stringify(form))
              this.$router.push({
                name: 'sales/coupon/select',
                query: {
                  couponId: this.couponId
                }
              })
              this.$emit('changeBtn', false)
            }).catch(() => {
              this.$emit('changeBtn', false)
              this.status = 'pass'
            })
          } else {
            let param = {
              couponPromotionType: this.form.couponPromotionType,
              couponDiscountType: this.form.couponDiscountType,
              amount: this.form.amount,
              couponName: this.form.couponName,
              couponStatus: this.form.couponStatus ? 'OPEN' : 'NO_OPEN',
              couponType: 'COUPON_PLATFORM', // 这边只提交工厂券
              isPublic: this.form.isPublic,
              receiveEndDate: this.form.receiveDate[1] ? formatDate(new Date(this.form.receiveDate[1]), true) : '',
              receiveLimitType: this.form.receiveLimitType,
              receiveLimitQuantity: this.form.receiveLimitType === 'LIMIT_NONE' ? 0 : this.form.receiveLimitType === 'LIMIT_ONE' ? this.form.oneNum : this.form.dayNum, //传给后台的领的数量
              receiveMethod: this.form.couponDiscountType === 'DENOMINATION_COUPON' ? this.form.receiveMethod : 'SELLER_SEND',
              receiveStartDate: this.form.receiveDate[0] ? formatDate(new Date(this.form.receiveDate[0]), true) : '',
              remark: this.form.remark,
              useCondition: this.form.couponDiscountType == 'DISCOUNT_COUPON' || this.form.couponDiscountType == 'CASH_COUPON' ? null : this.form.useCondition,
              discount: this.form.couponDiscountType == 'DISCOUNT_COUPON' ? this.form.discount : null,
              validityType: this.showDate ? 'ABSOLUTE_TIME' : 'RELATIVE_TIME',
              discountGroupList: this.form.couponDiscountType === 'DISCOUNT_COUPON' ? [this.form.discountGroupList[0]] : [], //打折的阶梯
              couponSendChannelEnum: this.form.couponSendChannelEnum,
              useQuantityPerProduct: this.form.couponDiscountType == 'DENOMINATION_COUPON' ? this.form.useQuantityPerProduct : null
            }
            // if (this.form.receiveDate[0] && this.form.receiveDate[1]) {
            //   param.receiveEndDate = formatDate(new Date(this.form.receiveDate[1]), true)
            //   param.receiveStartDate = formatDate(new Date(this.form.receiveDate[0]), true)
            // }
            if (this.showDate) {
              param.validityStartDate = formatDate(new Date(this.form.validityDate[0]), true)
              param.validityEndDate = formatDate(new Date(this.form.validityDate[1]), true)
            } else {
              param.validityDays = this.form.validityDays
            }
            if (this.form.showNum) {
              param.denomination = this.form.couponDiscountType == 'DISCOUNT_COUPON' ? null : this.form.diyMoney
            } else {
              param.denomination = this.form.couponDiscountType == 'DISCOUNT_COUPON' ? null : this.form._denomination
            }
            param.couponProductList = []
            // param = clearEmpty(param)
            this.$jsonPost('/coupon/coupon_info/save', param).then(data => {
              this.$Notice.success({
                title: '保存成功'
              })
              this.$emit('changeBtn', false)
              this.jumpPage()
            }).catch(() => {
              this.status = 'pass'
              this.$emit('changeBtn', false)
            })
          }
        } else {
          this.$emit('changeBtn', false)
        }
      })
    },
    jumpPage () {
      // sessionStorage.removeItem('couponForm')
      // sessionStorage.removeItem('originalForm')
      this.$router.push({
        name: 'sales/coupon/list'
      })
    },
    onChangeType (value) {
      this.form.maxLevel = Number(value) === 1 ? 5 : 1
      this.$emit('complete', value)
    },
    onChangeValidityType () {
      this.showDate = !this.showDate
    },
    onChangeDenomination (target) {
      if (target.value === 12) {
        this.$nextTick(() => {
          this.showNum = true
        })
      } else {
        this.$nextTick(() => {
          this.showNum = false
        })
        this.form._denomination = target.label
      }
    }
  }
}
</script>
